﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;
using DG.Tweening;
using GameControl;
using System;
public class SpringFistFunc : ItemFunc
{
    public float Power = 0;
    public float Range;//攻击范围
    protected GameObject Fist;
    public float Speed;
    private float StartX;
    protected string FistExtentName;

    public override void Awake()
    {
        base.Awake();
        Fist = this.transform.Find("Collider").Find("Fist").gameObject;
        StartX = Fist.transform.localPosition.x;
    }
    public override void KeyDownInvoke()
    {
        base.KeyDownInvoke();
        Fist.transform.DOLocalMove(new Vector3(Range, 0, 0), Speed);
    }
    public override void KeyUpInvoke()
    {
        base.KeyUpInvoke();
        Fist.transform.DOLocalMove(new Vector3(StartX, 0, 0), Speed);

    }
   public override void OnStart()
    {
        MessageManager.Instance.Get<GameMap.ItemMapEvent>().DispatchMessage(GameMap.ItemMapEvent.CreateInMap, this, new GameMap.ItemCreateArgs("MagicAttackExtent", ThisItem.ItemFieldSetting.Position, (ItemField x) =>
        {
            //  ((((e as UniversalEventArgs).Info) as Item).ItemFieldSetting as AttackExtentItemField).Power = 0;
            (x as AttackExtentItemField).Power = Power;
            FistExtentName = x.ItemName;
            x.Position = ThisItem.ItemFieldSetting.Position;
            GameManager.Get<IItemHealthManager>().IgnorePairs.UniqueAdd(new Tuple<string, string>(FistExtentName, ThisItem.ItemFieldSetting.ItemName));
            (x as AttackExtentItemField).TriggerColliderPoints =new List<Vector2>(Fist.GetComponent<PolygonCollider2D>().points);
            //       Debug.Log(handholdItemName + " " + ItemFieldSetting.ItemName);

        }));
    }
    public override void OnDestory()
    {
        base.OnDestory();
        if (FistExtentName != default)
        {
            //当自身销毁,连带销毁子物体
            MessageManager.Instance.Get<GameMap.ItemMapEvent>().DispatchMessage(GameMap.ItemMapEvent.DeleteInMap, this, new GameMap.ItemMapArgs { ItemName = FistExtentName }, null);
        }
        FistExtentName = default;
    }
    private void FixedUpdate()
    {
        RefreshHandHoldCollider();
    }
    public virtual void RefreshHandHoldCollider()
    {

        //    return;
        var item = ClockManager.Get<IItemSyncManager>().GetGameObjectByName(FistExtentName);
        if (item == null)
        {
            return;
        }
        UnsafeTool.RefreshPolygonColliderBySprite(item, Fist.GetComponent<SpriteRenderer>().sprite);
        item.transform.position = Fist.transform.position;
    }
}